/** @file    EncodersXmlQt_Automations.cpp
 *  @author  Alessandro Polo
 *  @version $Id: EncodersXmlQt_Automations.cpp 2770 2010-07-14 01:33:48Z alex $
 *  @brief
 * The header for this class can be found in EncodersXmlQt_Automations.h, check that file
 * for class description.
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/
 
 #include <framework/persistence/qt/EncodersXmlQt_Automations.h>

 #include <framework/persistence/qt/EncodersXmlQt_Core.h>
 #include <core/ObjectAllocator.h>


namespace wosh {
 namespace persistence {
  namespace qt {

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

 WOSH_ENCODER_XML_Qt_BIND(wosh::persistence::qt::EncoderXml_Qt_Automations_Automation, "wosh::automations::Automation", 0.0, _static_EncoderXml_Qt_Automations_Automation )

WRESULT EncoderXml_Qt_Automations_Automation::serializeTo( const wosh::automations::Automation* object, QDomDocument& doc, QDomElement& contNode, int encoder_options ) {
	if ( object == NULL ) return WRET_ERR_PARAM;
	WRESULT ret = EncoderXml_Qt_Object::serializeTo(object, doc, contNode, encoder_options );
	EncoderBaseXmlQt::serializeInt64( "id", object->getID(), doc, contNode, encoder_options );
	EncoderBaseXmlQt::serializeBool( "enabled", object->isEnabled(), doc, contNode, encoder_options );
	EncoderBaseXmlQt::serializeLong( "priority", object->getPriority(), doc, contNode, encoder_options );
	EncoderBaseXmlQt::serializeLong( "mode-mapping", (int)object->getModeMapping(), doc, contNode, encoder_options );

	QDomElement node_busses = doc.createElement("monitored-busses"); contNode.appendChild(node_busses);
	std::vector<std::string> busses; object->getMonitoredBusses(busses);
	EncoderBaseXmlQt::serializeVectorString( busses, doc, node_busses, encoder_options, "bus" );

	EncoderBaseXmlQt::serializeInt64( "last-executedtime", object->getLastExecutedTime(), doc, contNode, encoder_options );
	EncoderBaseXmlQt::serializeInt64( "last-trigger", object->getLastTrigger(), doc, contNode, encoder_options );

	QDomElement node_triggers = doc.createElement("triggers"); contNode.appendChild(node_triggers);
	EncoderBaseXmlQt::serializeMapT<wosh::MessageFilter,int, EncoderXml_Qt_Message, EncoderXml_Qt_Variant>(object->getTriggers(), doc, node_triggers, encoder_options );

	QDomElement node_effects = doc.createElement("effects"); contNode.appendChild(node_effects);
	EncoderBaseXmlQt::serializeListT<wosh::Message,EncoderXml_Qt_Message>(object->getEffects(), doc, node_effects, encoder_options );

	if ( isCompact(encoder_options) || !object->getProperties().isEmpty() ) {
		QDomElement node_properties = doc.createElement("properties"); contNode.appendChild(node_properties);
		ret += EncoderXml_Qt_PropertiesProvider::serializeTo(&object->getProperties(), doc, node_properties, encoder_options );
	 }
	return ret;
 }

WRESULT EncoderXml_Qt_Automations_Automation::deserializeFrom( wosh::automations::Automation* object, const QDomElement& contNode ) {
	if ( object == NULL ) return WRET_ERR_PARAM;
	WRESULT ret = EncoderXml_Qt_Object::deserializeFrom(object, contNode );
	object->setID( EncoderBaseXmlQt::deserializeInt64( "id", 0, contNode ) );
	object->setEnabled( EncoderBaseXmlQt::deserializeBool( "enabled", true, contNode ) );
	object->setPriority( EncoderBaseXmlQt::deserializeLong( "priority", 0, contNode ) );
	object->setModeMapping( (wosh::automations::Automation::MODE_EFFECT_MAPPING)EncoderBaseXmlQt::deserializeLong( "mode-mapping", (int)wosh::automations::Automation::MAPPING_UNKNOWN, contNode ) );

	QDomElement node_busses = contNode.namedItem("monitored-busses").toElement();
	if ( !node_busses.isNull() ) {
		std::vector<std::string> busses; 
		EncoderBaseXmlQt::deserializeVectorString( busses, node_busses);
		object->setMonitoredBusses(busses);
	 }

	QDomElement triggersNode = contNode.namedItem("triggers").toElement();
	if ( !triggersNode.isNull() ) {
		ret += EncoderBaseXmlQt::deserializeMapT<wosh::MessageFilter,int, EncoderXml_Qt_Message, EncoderXml_Qt_Variant>( object->getTriggers(), triggersNode, (int)MessageFilter::HEADER_CMP_VALID );
///@bug BUG! se extension of Message
	 }

	QDomElement effectsNode = contNode.namedItem("effects").toElement();
	if ( !effectsNode.isNull() ) {
		ret += EncoderBaseXmlQt::deserializeListT<wosh::Message,EncoderXml_Qt_Message>( object->getEffects(), effectsNode );
///@bug BUG! se extension of Message
	 }

	QDomElement node_properties = contNode.namedItem("properties").toElement();
	if ( !node_properties.isNull() )
		ret += EncoderXml_Qt_PropertiesProvider::deserializeFrom(&object->getProperties(), node_properties );
	return ret;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

 WOSH_ENCODER_XML_Qt_BIND(wosh::persistence::qt::EncoderXml_Qt_Automations_CronJob, "wosh::automations::CronJob", 0.0, _static_EncoderXml_Qt_Automations_CronJob )

WRESULT EncoderXml_Qt_Automations_CronJob::serializeTo( const wosh::automations::CronJob* object, QDomDocument& doc, QDomElement& contNode, int encoder_options ) {
	if ( object == NULL ) return WRET_ERR_PARAM;
	WRESULT ret = EncoderXml_Qt_Automations_Automation::serializeTo(object, doc, contNode, encoder_options );
	QDomElement node_schedule = doc.createElement("schedule"); contNode.appendChild(node_schedule);
	EncoderBaseXmlQt::serializeLong( "year", object->getYear(), doc, node_schedule, true );
	EncoderBaseXmlQt::serializeLong( "month", object->getMonth(), doc, node_schedule, true );
	EncoderBaseXmlQt::serializeLong( "day", object->getDay(), doc, node_schedule, true );
	EncoderBaseXmlQt::serializeLong( "hour", object->getHour(), doc, node_schedule, true );
	EncoderBaseXmlQt::serializeLong( "min", object->getMin(), doc, node_schedule, true );
	EncoderBaseXmlQt::serializeLong( "sec", object->getSec(), doc, node_schedule, true );
	return ret;
 }

WRESULT EncoderXml_Qt_Automations_CronJob::deserializeFrom( wosh::automations::CronJob* object, const QDomElement& contNode ) {
	if ( object == NULL ) return WRET_ERR_PARAM;
	WRESULT ret = EncoderXml_Qt_Automations_Automation::deserializeFrom(object, contNode );
	QDomElement node_schedule = contNode.namedItem("schedule").toElement();
	if ( !node_schedule.isNull() ) {
		int year = EncoderBaseXmlQt::deserializeLong( "year", -1, node_schedule );
		int month = EncoderBaseXmlQt::deserializeLong( "month", -1, node_schedule );
		int day = EncoderBaseXmlQt::deserializeLong( "day", -1, node_schedule );
		object->setDate( year, month, day );
		int hour = EncoderBaseXmlQt::deserializeLong( "hour", -1, node_schedule );
		int min = EncoderBaseXmlQt::deserializeLong( "min", 0, node_schedule );
		int sec= EncoderBaseXmlQt::deserializeLong( "sec", 0, node_schedule );
		object->setTime( hour, min, sec );
	 }
	return ret;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

  }; // namespace qt
 }; // namespace persistence
}; // namespace wosh
